package com.andy.toast.utils

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.CheckResult
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.andy.base.AppHelper
import com.andy.toast.R
import com.andy.toast.extend.colorFromRes

object ToastUtils {
    @ColorInt
    private val DEFAULT_TEXT_COLOR = com.andy.base.R.color.white.colorFromRes()

    @ColorInt
    private val SUCCESS_TEXT_COLOR = com.andy.base.R.color.white.colorFromRes()

    @ColorInt
    private val ERROR_TEXT_COLOR = com.andy.base.R.color.color_2a2843.colorFromRes()

    @DrawableRes
    private val DEFAULT_BACKGROUND_DRAWABLE_RES = R.drawable.bg_shape_r10_black

    @DrawableRes
    private val SUCCESS_ICON_RES = R.drawable.ic_toast_success

    @DrawableRes
    private val SUCCESS_BACKGROUND_DRAWABLE_RES = R.drawable.bg_shape_r10_black

    @DrawableRes
    private val ERROR_ICON_RES = R.drawable.ic_toast_error

    @DrawableRes
    private val ERROR_BACKGROUND_DRAWABLE_RES = R.drawable.bg_shape_r10_light_red

    //普通提示
    fun normal(message: String) {
        normal(AppHelper.mContext, message, Toast.LENGTH_SHORT).show()
    }

    fun normal(message: String, duration: Int) {
        normal(AppHelper.mContext, message, duration).show()
    }

    //成功提示
    fun success(message: String) {
        success(AppHelper.mContext, message, Toast.LENGTH_SHORT, true).show()
    }

    fun success(message: String, duration: Int) {
        success(AppHelper.mContext, message, duration, true).show()
    }

    //错误提示
    fun error(message: String) {
        error(AppHelper.mContext, message, Toast.LENGTH_SHORT, true).show()
    }

    fun error(message: String, duration: Int) {
        error(AppHelper.mContext, message, duration, true).show()
    }

    @CheckResult
    private fun normal(
        context: Context, message: String, duration: Int
    ): Toast {
        return custom(context, message, -1, DEFAULT_TEXT_COLOR, DEFAULT_BACKGROUND_DRAWABLE_RES, duration, false)
    }

    @CheckResult
    private fun success(
        context: Context, message: String, duration: Int, withIcon: Boolean
    ): Toast {
        return custom(context, message, SUCCESS_ICON_RES, SUCCESS_TEXT_COLOR, SUCCESS_BACKGROUND_DRAWABLE_RES, duration, withIcon)
    }

    @CheckResult
    private fun error(context: Context, message: String, duration: Int, withIcon: Boolean): Toast {
        return custom(context, message, ERROR_ICON_RES, ERROR_TEXT_COLOR, ERROR_BACKGROUND_DRAWABLE_RES, duration, withIcon)
    }

    @CheckResult
    private fun custom(
        context: Context, message: String, @DrawableRes iconResId: Int, @ColorInt textColor: Int, @DrawableRes backgroundRes: Int, duration: Int, withIcon: Boolean
    ): Toast {
        return customToast(context, message, iconResId, textColor, backgroundRes, duration, withIcon)
    }

    @CheckResult
    private fun customToast(
        context: Context, message: String, @DrawableRes iconResId: Int, @ColorInt textColor: Int, @DrawableRes backgroundResId: Int, duration: Int, withIcon: Boolean
    ): Toast {
        val currentToast = Toast(context)
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val toastLayout = inflater.inflate(R.layout.toast_layout, null)
        val toastIcon = toastLayout.findViewById<ImageView>(R.id.toast_icon)
        val toastTextView = toastLayout.findViewById<TextView>(R.id.toast_text)
        setBackground(toastLayout, backgroundResId)
        if (withIcon) {
            setBackground(toastIcon, iconResId)
        } else {
            toastIcon.visibility = View.GONE
        }
        toastTextView.setTextColor(textColor)
        toastTextView.text = message
        currentToast.view = toastLayout
        currentToast.duration = duration
        return currentToast
    }

    private fun setBackground(view: View, resId: Int?) {
        if (resId != null) {
            view.background = ContextCompat.getDrawable(AppHelper.mContext, resId)
        }
    }
}